/**
 * 
 */
package com.youth.fund.hk.hostManager.shiro.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.authz.permission.WildcardPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.youth.fund.hk.hostManager.common.dao.ICommonDao;
import com.youth.fund.hk.hostManager.common.service.impl.CommonService;
import com.youth.fund.hk.hostManager.shiro.dao.ResourceDao;
import com.youth.fund.hk.hostManager.shiro.datamodel.Resource;
import com.youth.fund.hk.hostManager.shiro.service.ResourceService;

/**
 * @author Sunk_Sky
 *
 *         2014年11月13日下午6:46:04
 */
@Service("ResourceService")
public class ResourceServiceImpl extends CommonService<Resource> implements
		ResourceService
{
	private ResourceDao resourceDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.youth.fund.hk.hostManager.common.service.impl.CommonService#setCommonDao
	 * (com.youth.fund.hk.hostManager.common.dao.ICommonDao)
	 */
	@Autowired
	@Qualifier("ResourceDao")
	@Override
	public void setCommonDao(ICommonDao<Resource> commonDao)
	{
		this.commonDao = commonDao;

		this.resourceDao = (ResourceDao) commonDao;
	}

	public Resource createResource(Resource resource)
	{
		return resourceDao.createResource(resource);
	}

	public Resource updateResource(Resource resource)
	{
		return resourceDao.updateResource(resource);
	}

	public void deleteResource(Long resourceId)
	{
		resourceDao.deleteResource(resourceId);
	}

	public Resource findOne(Long resourceId)
	{
		return resourceDao.findOne(resourceId);
	}

	public List<Resource> findAll()
	{
		return resourceDao.findAll();
	}

	public Set<String> findPermissions(Set<Long> resourceIds)
	{
		Set<String> permissions = new HashSet<String>();
		for (Long resourceId : resourceIds)
		{
			Resource resource = findOne(resourceId);
			if (resource != null
					&& !StringUtils.isEmpty(resource.getPermission()))
			{
				permissions.add(resource.getPermission());
			}
		}
		return permissions;
	}

	public List<Resource> findMenus(Set<String> permissions)
	{
		List<Resource> allResources = findAll();
		List<Resource> menus = new ArrayList<Resource>();
		for (Resource resource : allResources)
		{
			if (resource.isRootNode())
			{
				continue;
			}
			if (resource.getType() != Resource.ResourceType.menu)
			{
				continue;
			}
			if (!hasPermission(permissions, resource))
			{
				continue;
			}
			menus.add(resource);
		}
		return menus;
	}

	private boolean hasPermission(Set<String> permissions, Resource resource)
	{
		if (StringUtils.isEmpty(resource.getPermission()))
		{
			return true;
		}
		for (String permission : permissions)
		{
			WildcardPermission p1 = new WildcardPermission(permission);
			WildcardPermission p2 = new WildcardPermission(
					resource.getPermission());
			if (p1.implies(p2) || p2.implies(p1))
			{
				return true;
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.service.impl.CommonService#generateHtml(java.io.Serializable)
	 */
	@Override
	public void generateHtml(Resource model)
	{
		// TODO Auto-generated method stub
		
	}
}
